<!--
 * @Descripttion: (工艺主表/cks_technique)
 * @Author: (admin)
 * @Date: (2025-05-12)
-->
<template>
  <div>
    <el-form :model="queryParams" label-position="right" inline ref="queryRef" v-show="showSearch" @submit.prevent>
      <el-form-item>
        <el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
        <el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
      </el-form-item>
    </el-form>
    <!-- 工具区域 -->
    <el-row :gutter="15" class="mb10">
      <el-col :span="1.5">
        <el-button type="primary" v-hasPermi="['ckstechnique:add']" plain icon="plus" @click="handleAdd">
          {{ $t('btn.add') }}
        </el-button>
      </el-col>
    </el-row>

    <el-row style="flex: 1;display: flex; ">
      <!-- 工站列表 ，选择不同工站需要切换动态表格 -->
      <el-col :span="24" :xs="24" style="display: flex;">
        <div class="vertical-options">
          <ul>
            <li v-for="option in templateOptions" :key="option.id" :class="{ 'selected': selectedValue === option.id }"
              @click="selectedValue = option.id">
              {{ option.name }}
            </li>
          </ul>
        </div>

        <!-- 动态表格区域 -->
        <div class="table-container">

          <el-table :data="dataList" v-loading="loading" ref="table" border
            header-cell-class-name="el-table-header-cell" highlight-current-row @sort-change="sortChange">

            <!-- 显示列 -->
            <el-table-column v-for="col in columns.filter(o => o.visible)" :key="col.prop" :prop="col.prop"
              :label="col.label" align="center" :width="col.width || 'auto'" :show-overflow-tooltip="true" >
              <!-- 工艺名称 -->
              <template #default="scope" v-if="col.prop==='name'">
                <el-link type="primary" @click="showDetailsData(scope.row)">{{ scope.row.name }}</el-link>
              </template>

              <!-- 启用&禁用 -->
              <template #default="scope" v-if="col.prop==='nullity'">
                {{ scope.row.nullity ? $t('common.disable') : $t('common.enable') }}
              </template>
            </el-table-column>
           
            <el-table-column :label="$t('btn.operate')" align="center">
              <template #default="scope">
                <el-button type="success" size="small" icon="edit" :title="$t('btn.edit')"
                  v-hasPermi="['ckstechnique:edit']" @click="handleUpdate(scope.row)">{{$t('btn.edit')}}</el-button>
                <el-button type="danger" size="small" icon="delete" :title="$t('btn.delete')"
                  v-hasPermi="['ckstechnique:delete']" @click="handleDelete(scope.row)">{{$t('btn.delete')}}</el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
            @pagination="getList" />
        </div>
      </el-col>
    </el-row>

    <!-- 添加或修改工艺主表对话框 -->
    <el-dialog :title="title" :lock-scroll="false" v-model="open">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="120px">
        <el-row :gutter="20">

          <el-col :lg="12">
            <el-form-item :label="$t('process.ckstechniquePage.tableColumns.processName')" prop="name">
              <el-input v-model="form.name" :placeholder="$t('process.ckstechniquePage.formDialog.processName')" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item :label="$t('process.ckstechniquePage.tableColumns.processCode')" prop="code">
              <el-input v-model="form.code" :placeholder="$t('process.ckstechniquePage.formDialog.processCode')" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item :label="$t('process.ckstechniquePage.tableColumns.remark')" prop="notes">
              <el-input v-model="form.notes" :placeholder="$t('process.ckstechniquePage.formDialog.remark')" />
            </el-form-item>
          </el-col>

        </el-row>
      </el-form>

      <template #footer v-if="opertype != 3">
        <el-button text @click="cancel">{{ $t('btn.cancel') }}</el-button>
        <el-button type="primary" @click="submitForm">{{ $t('btn.submit') }}</el-button>
      </template>
    </el-dialog>

    <el-dialog v-model="techDataVisible" draggable width="60%" :lock-scroll="false">
      <detail-info v-model:tech="tech"></detail-info>
    </el-dialog>

  </div>
</template>

<script setup name="ckstechnique">
import detailInfo from '@/views/business/CksTechnique/components/detailInfo'

import {
  listCksTechnique,
  addCksTechnique, delCksTechnique,
  updateCksTechnique, getCksTechnique,
}
  from '@/api/business/ckstechnique.js'

import request, { downFile } from '@/utils/request'
const { proxy } = getCurrentInstance()
const ids = ref([])
const loading = ref(false)
const showSearch = ref(true)
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  sort: '',
  sortType: 'asc',
})
const columns = ref([
  { visible: false, prop: 'id', label: '工艺Id' },
  { visible: true, prop: 'name', label: proxy.$t('process.ckstechniquePage.tableColumns.processName') },
  { visible: true, prop: 'code', label: proxy.$t('process.ckstechniquePage.tableColumns.processCode') },
  // { visible: false, prop: 'type', label: '工艺类型' },
  { visible: true, prop: 'notes', label: proxy.$t('process.ckstechniquePage.tableColumns.remark') },
  { visible: true, prop: 'lastSendTime', label: proxy.$t('process.ckstechniquePage.tableColumns.lastSendTime') },
  // { visible: false, prop: 'templateId', label: '工站模板Id' },
  // { visible: true, prop: 'plcId', label: 'Plc编号' },
  // { visible: true, prop: 'actionId', label: '动作编号' },
  { visible: true, prop: 'nullity', label: proxy.$t('process.ckstechniquePage.tableColumns.runStatus') },
  // { visible: false, prop: 'queryStatus', label: '查询状态(0默认 1未使用 2使用中  )' },
  // { visible: false, prop: 'createdTime', label: 'CreatedTime' },
  // { visible: false, prop: 'isDeleted', label: 'IsDeleted' },
])
const total = ref(0)
const dataList = ref([])
const queryRef = ref()
const defaultTime = ref([new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)])


var dictParams = [
]
const selectedValue = ref(null);
const templateOptions = ref([])
function getList() {
  loading.value = true

  queryParams.TemplateId = selectedValue.value
  listCksTechnique(queryParams).then(res => {
    const { code, data } = res
    if (code == 200) {
      dataList.value = data.result
      total.value = data.totalNum
      loading.value = false
    }
  })
}

// 查询
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

// 重置查询操作
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}
// 自定义排序
function sortChange(column) {
  var sort = undefined
  var sortType = undefined

  if (column.prop != null && column.order != null) {
    sort = column.prop
    sortType = column.order

  }
  queryParams.sort = sort
  queryParams.sortType = sortType
  handleQuery()
}

/*************** form操作 ***************/
const formRef = ref()
const title = ref('')
// 操作类型 1、add 2、edit 3、view
const opertype = ref(0)
const open = ref(false)
const state = reactive({
  single: true,
  multiple: true,
  form: {},
  rules: {
    name: [{ required: true, message: proxy.$t('process.ckstechniquePage.formDialogVerify.processName'), trigger: "blur" }],
    code: [{ required: true, message: proxy.$t('process.ckstechniquePage.formDialogVerify.processCode'), trigger: "blur" }],
  },
})

const { form, rules, options, single, multiple } = toRefs(state)

// 关闭dialog
function cancel() {
  open.value = false
  reset()
}

// 重置表单
function reset() {
  form.value = {
    id: 0,
    name: null,
    code: null,
    type: 0,
    lastSendTime: null,
    notes: '',
    templateId: selectedValue.value,
    nullity: null,
    queryStatus: null,
    createdTime: null,
    isDeleted: null,
  };
  proxy.resetForm("formRef")
}


// 添加按钮操作
function handleAdd() {
  reset();
  open.value = true
  title.value = proxy.$t('process.ckstechniquePage.formDialog.addTitle')
  opertype.value = 1
}
// 修改按钮操作
function handleUpdate(row) {
  reset()
  const id = row.id || ids.value
  getCksTechnique(id).then((res) => {
    const { code, data } = res
    if (code == 200) {
      open.value = true
      title.value = proxy.$t('process.ckstechniquePage.formDialog.editTitle')
      opertype.value = 2
      data.templateId = selectedValue.value
      form.value = {
        ...data,
      }
    }
  })
}

// 添加&修改 表单提交
function submitForm() {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {

      if (form.value.id != undefined && opertype.value === 2) {
        updateCksTechnique(form.value).then((res) => {
          proxy.$modal.msgSuccess(proxy.$t('common.operateSuccess'))
          open.value = false
          getList()
        })
      } else {
        addCksTechnique(form.value).then((res) => {
          proxy.$modal.msgSuccess(proxy.$t('common.operateSuccess'))
          open.value = false
          getList()
        })
      }
    }
  })
}

// 删除按钮操作
function handleDelete(row) {
  const Ids = row.id || ids.value

  proxy
    .$confirm(proxy.$t("common.deleteConfirm",[Ids]), proxy.$t("common.systemTips"), {
      confirmButtonText: proxy.$t('common.ok'),
      cancelButtonText: proxy.$t('common.cancel'),
      type: "warning",
    })
    .then(function () {
      return delCksTechnique(Ids)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess(proxy.$t("common.operateSuccess"))
    })
}
// 工艺Id传值给子组件
const tech = ref(null)
// 工艺明细弹出层
const techDataVisible = ref(false)
function showDetailsData(row) {
  tech.value = row
  techDataVisible.value = true
}

watch(techDataVisible, (newa, old) => {
  if (old && !newa) {
    getList()
  }
})
watch(selectedValue, (a, b) => {

  //查询当前表格数据，动态列渲染
  if (selectedValue.value) {
    queryParams.pageNum = 1
    handleQuery()
  }
})


onMounted(() => {
  loaded()
})

function loaded() {
  //查询工站信息
  request({
    url: '/business/CksTechnique/GetTemplates',
    method: 'post'
  }).then((res) => {
    // debugger
    templateOptions.value = res.data
    if (res.data.length > 0)
      selectedValue.value = res.data[0].id

  })
}






</script>
<style scoped>
.vertical-options ul {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-wrap: nowrap;
  padding-right: 10px;
  flex-direction: column;
}

.vertical-options li {
  padding: 10px 15px;
  display: inline-block;
  background-color: white;
  cursor: pointer;
  border-radius: 4px;
  transition: all 0.3s ease;
  border: 1px solid #eee;
  /* 外边框 */
  white-space: nowrap;
  margin-top: 3px;
  max-width: 300px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.vertical-options li:hover {
  background-color: #f5f5f5;
  border-bottom: 1px solid #eee;
  /* 浅灰色边框 */
}

/* 最后一个li不需要底部边框 */
/* .vertical-options li:last-child {
  border-bottom: none;
} */

.vertical-options li.selected {
  background-color: #1e90ff;
  color: white;
}

.table-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
  padding-bottom: 10px;
  gap: 5px;
  box-sizing: border-box;
}

.table-fill {

  position: absolute;
  height: 100%;
  width: 100%;
  overflow: hidden;
}
</style>
